//
//  ViewController.m
//  GCD
//
//  Created by jayce on 17/9/20.
//  Copyright © 2017年 jjkj. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIButton *btn;
    btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 150, 100, 30);
    [btn setTitle:@"GCD" forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor greenColor]];
    [btn addTarget:self action:@selector(clickGCD) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (void)clickGCD {
    
    NSLog(@"执行GCG");
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       
        NSLog(@"Start task 1");
        
        [NSThread sleepForTimeInterval:3];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            //回到主线程刷新UI
            NSLog(@"刷新UI");
        });

    });
    
    
    
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        
//        NSLog(@"start task 1");
//        
//        [NSThread sleepForTimeInterval:2];
//        
//        NSLog(@"end task 1");
//        
//    });
//    
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
//        
//        NSLog(@"start task 2");
//        
//        [NSThread sleepForTimeInterval:2];
//        
//        NSLog(@"end task 2");
//        
//    });
//    
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
//        
//        NSLog(@"start task 3");
//        
//        [NSThread sleepForTimeInterval:2];
//        
//        NSLog(@"end task 3");
//        
//    });
    
    
    
//    //NULL == DISPATCH_QUEUE_SERIAL
//   dispatch_queue_t queue = dispatch_queue_create("com.test.gcg.queue", DISPATCH_QUEUE_CONCURRENT);
//    
//    dispatch_async(queue, ^{
//        
//        NSLog(@"start task 1");
//        
//        [NSThread sleepForTimeInterval:2];
//        
//        NSLog(@"end task 1");
//    });
//    
//    dispatch_async(queue, ^{
//        
//        NSLog(@"start task 2");
//        
//        [NSThread sleepForTimeInterval:2];
//        
//        NSLog(@"end task 2");
//    });
//    
//    dispatch_async(queue, ^{
//        
//        NSLog(@"start task 3");
//        
//        [NSThread sleepForTimeInterval:2];
//        
//        NSLog(@"end task 3");
//    });
    
    
    
    
    
//    dispatch_group_notify(group, queue1, ^{
//        
//        NSLog(@"All tasks over");
//        
//        dispatch_async(dispatch_get_main_queue(), ^{
//            
//            NSLog(@"回到主线程刷新UI");
//        });
//    });
}


- (void)sendRequest1:(void(^)())block {
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSLog(@"start task 1");
        
        [NSThread sleepForTimeInterval:2];
        
        NSLog(@"end task 1");
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (block) {
                block();
            }
        });
    });
}

- (void)sendRequest2:(void(^)())block {
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSLog(@"start task 2");
        
        [NSThread sleepForTimeInterval:2];
        
        NSLog(@"end task 2");
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (block) {
                block();
            }
        });
    });
}

- (void)gcd_enterAndLeave {
  
//    dispatch_group_enter(group);
//    [self sendRequest1:^{
//        NSLog(@"request1 done");
//        dispatch_group_leave(group);
//    }];
//    
//    dispatch_group_enter(group);
//    [self sendRequest1:^{
//        NSLog(@"request2 done");
//        dispatch_group_leave(group);
//    }];
    
}

- (void) gcd_netRequest {
    
    //    dispatch_group_async(group, queue1, ^{
    //
    //        [self sendRequest1:^{
    //            NSLog(@"request1 done");
    //        }];
    //    });
    //
    //    dispatch_group_async(group, queue1, ^{
    //
    //        [self sendRequest2:^{
    //            NSLog(@"request2 done");
    //        }];
    //    });
}

- (void)gcd_group {
    
//    dispatch_queue_t queue1 = dispatch_queue_create("com.test.gcg.group", DISPATCH_QUEUE_CONCURRENT);
//    
//    dispatch_group_t group = dispatch_group_create();
//    
//    
//    dispatch_group_async(group, queue1, ^{
//        
//        NSLog(@"start task 1");
//        
//        [NSThread sleepForTimeInterval:2];
//        
//        NSLog(@"end task 1");
//    });
//    
//    dispatch_group_async(group, queue1, ^{
//        
//        NSLog(@"start task 2");
//        
//        [NSThread sleepForTimeInterval:2];
//        
//        NSLog(@"end task 2");
//    });
//    
//    dispatch_group_async(group, queue1, ^{
//        
//        NSLog(@"start task 3");
//        
//        [NSThread sleepForTimeInterval:2];
//        
//        NSLog(@"end task 3");
//    });

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
